<template>
  <div>
    <BhHeader
      v-if="userInfo"
      :logo="logoUrl"
      :title="userTitle"
      :icons="icons"
      :navItems="menus"
      :slogon="slogon"
      :userInfo="userInfo"
      @role-change="roleChange"
    />
    <main class="sc-container-outerFrame">
      <article
        class="sc-container bh-mb-24"
        bh-layout-role="single"
        bh-container-role=container
        :class="{'hide-header': !showHeader}"
      >
        <!--标题-->
        <h2 v-if="showHeader" class="bh-paper-pile-dialog-headerTitle">{{title}}</h2>
        <!--内容-->
        <section id="content">
          <router-view></router-view>
        </section>
      </article>
    </main>
    <BhFooter copyright="版权信息：© 2020 江苏金智教育信息股份有限公司 苏ICP备10204514号" />
  </div>
</template>

<script>
import { BhHeader, BhFooter } from 'bh-vue'
import { getMenuInfo } from 'utils'
import { request } from 'utils/http'

let appTitle = ""
const makeRoutes = (menus) => {
  return menus && menus.map(item => {
    switch(item.type) {
      case 'form': {
        if(!appTitle) {
          appTitle = item.title
        }
        item.router = '/form/' + item.title
        item.href = '#' + item.router
        break
      }
      case 'grid': {
        if(!appTitle) {
          appTitle = item.title
        }
        item.router = '/grid/' + item.title
        item.href = '#' + item.router
        break
      }
      case 'url': {
        item.href = item.path
        item.isOpenNewPage = true
        break
      }
      default: {
        item.router = '/subpage/' + item.title
        item.href = '#' + item.router
      }
    }

    if(item.sub) {
      item.children = makeRoutes(item.sub)
    }

    return item
  })
}

export default {
  name: 'app',
  data () {
    return {
      title: '',
      userTitle: "用户信息",
      menus: makeRoutes(window.menu),
      slogon: window.appName,
      icons: [],
      userInfo: null,
      logoUrl: window.resPath + "/scenes/public/images/demo/logo.png"
    }
  },
  computed: {
    showHeader () {
      return this.menuInfo.type === 'grid'
    },
    menuInfo () {
      if (!this.title || !window.menu) {
        return {}
      }

      return getMenuInfo(this.title) || {}
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.title = vm.title || to.params.name
    })
  },
  beforeRouteUpdate (to, from, next) {
    next()
    this.title = to.params.name
  },
  methods: {
    roleChange(val){
      console.log(val, 'roleChange')
    },
    getUserInfo(){
      const _this = this
      var getConfigUrl = window["tg-turing"].utils.getContextPath() + "/sys/emapturing/getConfig.do";
      request(getConfigUrl).then(res => {
        const data = res.USERINFO
        _this.userInfo = {
          image: data.userImage || 'http://res.wisedu.com/scenes/public/images/demo/user1.png',
          logoutHref: data.logoutUrl,
          info: [data.userName]
        }
      })
    }
  },
  components: {
    BhHeader, BhFooter
  },
  mounted() {
    if(this.$route.name === "home") {
      // this.$router.replace("/form/" + appTitle)
      if (window.menu) {
        this.$router.replace(window.menu[0].router)
      }
    }

    if (!this.title) {
      this.title = this.$route.params.name
    }

    this.getUserInfo()
  },
  created() {
    if(window.helpLink){
      this.icons = [{"className": "icon-helpoutline","attrs": {"bh-header-role": "appDetail"},href: window.helpLink,target: "_blank",title:'帮助'}]
    }
  },
}
</script>

<style scoped>
  .sc-container.hide-header {
    padding-top: 20px;
  }
</style>
<style>
  body{
    font-size: 12px !important;
  }
  .ivu-message{
    z-index: 9999 !important;
  }
  .bh-dialog-content .content{
    padding: 0px 5px 0px 0px;
  }
  .bh-dialog-content p {
    margin: 0px !important;
    text-align: left !important;
  }
</style>

